# -*- coding: utf-8 -*-
"""
作者: 肖麒
联系方式：QQ：1505448035
时间: 2025/8/13 17:44
文件作用：
"""
import sys
from PyQt6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
                             QListWidget, QLabel, QFrame, QListWidgetItem)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon, QPainter, QPixmap, QColor, QLinearGradient


class DarkTheme(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('深色主题风格 - 剪贴板助手')
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        self.setWindowFlags(
            Qt.WindowType.FramelessWindowHint |
            Qt.WindowType.WindowStaysOnTopHint
        )

        # 窗口高度为屏幕高度的28%
        screen = QApplication.primaryScreen()
        screen_rect = screen.geometry()
        window_height = int(screen_rect.height() * 0.20)

        self.setGeometry(0, screen_rect.height() - window_height,
                         screen_rect.width(), window_height)

        # 创建主容器
        self.main_container = QFrame(self)
        self.main_container.setObjectName("MainContainer")
        self.main_container.setGeometry(self.rect())

        # 动态计算字体大小
        base_font_size = int(screen_rect.width() * 0.007)
        min_font_size = 10
        max_font_size = 14
        font_size = max(min(base_font_size, max_font_size), min_font_size)

        # 统一圆角
        border_radius = 6

        # 深色主题风格样式表
        self.setStyleSheet(f"""
            #MainContainer {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(30, 30, 30, 0.95), stop:1 rgba(40, 40, 40, 0.95));
                border-radius: {border_radius}px;
                box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
                backdrop-filter: blur(12px);
                margin: 0px;
                padding: 0px;
            }}
            QWidget {{
                color: #e0e0e0;
                font-family: 'Segoe UI', 'Inter', 'Roboto', sans-serif;
                font-size: {font_size}px;
            }}
            /* 分组按钮样式 */
            QPushButton {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(50, 50, 50, 0.9), stop:1 rgba(60, 60, 60, 0.9));
                border: 1px solid rgba(75, 75, 75, 0.8);
                border-radius: {border_radius}px;
                padding: 8px 16px;
                min-height: 36px;
                margin: 5px;
                transition: all 0.3s ease;
                font-weight: 500;
                font-size: {font_size + 1}px;
                box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
            }}
            QPushButton:hover {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(60, 60, 60, 0.9), stop:1 rgba(70, 70, 70, 0.9));
                border-color: rgba(90, 90, 90, 0.9);
                transform: translateY(-2px);
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
            }}
            QPushButton:pressed {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(70, 70, 70, 0.9), stop:1 rgba(80, 80, 80, 0.9));
                transform: translateY(0);
            }}
            QPushButton:checked {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(80, 80, 80, 0.9), stop:1 rgba(90, 90, 90, 0.9));
                color: #e0e0e0;
                border-color: rgba(100, 100, 100, 0.8);
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
            }}
            QLabel {{
                color: #b0b0b0;
            }}
            /* 菜单图标样式 */
            QPushButton[iconOnly="true"] {{
                min-width: 36px;
                max-width: 36px;
                min-height: 36px;
                max-height: 36px;
                border-radius: {border_radius}px;
                padding: 0px;
                margin: 0 5px;
                font-size: 16px;
                background-color: rgba(55, 55, 55, 0.9);
                border: 1px solid rgba(75, 75, 75, 0.8);
            }}
            .delete-icon {{
                color: #fc8181;
            }}
            .settings-icon {{
                color: #a0aec0;
            }}
            .add-icon {{
                color: #68d391;
            }}
        """)

        # 创建主布局
        main_layout = QVBoxLayout(self.main_container)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)

        # 顶部分组栏
        self.group_bar_layout = QHBoxLayout()
        self.group_bar_layout.setSpacing(5)
        self.group_bar_layout.setContentsMargins(16, 8, 16, 0)  # 底部不留边距，由list_container控制

        # 添加分组按钮
        groups = ["临时分组", "建联", "未验签", "生成证书", "不清楚两个证书", "图片"]
        for group_name in groups:
            btn = QPushButton(group_name)
            btn.setCheckable(True)
            btn.setMinimumWidth(90)
            self.group_bar_layout.addWidget(btn)
            if group_name == "不清楚两个证书":
                btn.setChecked(True)

        # 右侧功能按钮区
        self.tools_layout = QHBoxLayout()
        self.tools_layout.setSpacing(5)

        # 删除按钮
        clear_btn = QPushButton("🗑️")
        clear_btn.setProperty("iconOnly", "true")
        clear_btn.setToolTip("清空当前分组")
        clear_btn.setObjectName("delete-icon")

        # 设置按钮
        settings_btn = QPushButton("⚙️")
        settings_btn.setProperty("iconOnly", "true")
        settings_btn.setToolTip("设置")
        settings_btn.setObjectName("settings-icon")

        # 添加分组按钮
        add_group_btn = QPushButton("➕")
        add_group_btn.setProperty("iconOnly", "true")
        add_group_btn.setToolTip("添加分组")
        add_group_btn.setObjectName("add-icon")

        self.tools_layout.addWidget(clear_btn)
        self.tools_layout.addWidget(settings_btn)
        self.tools_layout.addWidget(add_group_btn)

        self.group_bar_layout.addStretch()
        self.group_bar_layout.addLayout(self.tools_layout)

        # 中间内容区域 - 添加明确的10px边距
        self.item_list = ContentListWidget(self)
        self.item_list.setMinimumHeight(int(window_height * 0.75))

        # 添加示例内容
        self.add_sample_content()

        # 为列表容器添加10px上下边距（与菜单和底部的距离）
        list_container = QWidget()
        list_layout = QVBoxLayout(list_container)
        list_layout.setContentsMargins(16, 10, 16, 10)  # 上下边距设为10px，左右16px
        list_layout.addWidget(self.item_list)

        main_layout.addLayout(self.group_bar_layout)
        main_layout.addWidget(list_container)

    def add_sample_content(self):
        """添加示例内容"""
        sample_contents = [
            """超管申请生成的商户API证书压缩包中包含以下文件：
apiclient_key.pem：商户API证书私钥文件
apiclient_cert.pem：商户API证书公钥文件
apiclient_cert.p12：用于生成API证书的文件""",
            """因为贵司是首批平台证书过期的商户，需要尽快更新。
平台证书用于V3接口应答与回调的验签。
API证书就是所有V3接口都要用到，包括退款、支付等。
这是两个证书具体区别的文档：https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay5_0.shtml""",
            "Process finished with exit code -1",
            "D:\\software\\project\\acceptance-project\\自媒体\\剪贴板助手"
        ]

        for i, content in enumerate(sample_contents):
            list_item = QListWidgetItem()
            list_item.setText(content)

            # 设置正方形图标
            pixmap = QPixmap(100, 100)  # 正方形图标
            gradient = QLinearGradient(0, 0, 100, 100)

            if i == 0:
                gradient.setColorAt(0, QColor(17, 24, 39))
                gradient.setColorAt(1, QColor(31, 41, 55))
                pixmap.fill(QColor(17, 24, 39))
                painter = QPainter(pixmap)
                painter.setBrush(gradient)
                painter.setPen(QColor(148, 163, 184))
                painter.drawRoundedRect(0, 0, 100, 100, 6, 6)
                painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "📄 证书")
            elif i == 1:
                gradient.setColorAt(0, QColor(17, 24, 39))
                gradient.setColorAt(1, QColor(31, 41, 55))
                pixmap.fill(QColor(17, 24, 39))
                painter = QPainter(pixmap)
                painter.setBrush(gradient)
                painter.setPen(QColor(16, 185, 129))
                painter.drawRoundedRect(0, 0, 100, 100, 6, 6)
                painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "📋 说明")
            elif i == 2:
                gradient.setColorAt(0, QColor(17, 24, 39))
                gradient.setColorAt(1, QColor(31, 41, 55))
                pixmap.fill(QColor(17, 24, 39))
                painter = QPainter(pixmap)
                painter.setBrush(gradient)
                painter.setPen(QColor(249, 115, 22))
                painter.drawRoundedRect(0, 0, 100, 100, 6, 6)
                painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "🔄 进程")
            else:
                gradient.setColorAt(0, QColor(17, 24, 39))
                gradient.setColorAt(1, QColor(31, 41, 55))
                pixmap.fill(QColor(17, 24, 39))
                painter = QPainter(pixmap)
                painter.setBrush(gradient)
                painter.setPen(QColor(239, 68, 68))
                painter.drawRoundedRect(0, 0, 100, 100, 6, 6)
                painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "📂 路径")

            painter.end()
            list_item.setIcon(QIcon(pixmap))

            self.item_list.addItem(list_item)

    def update_item_size(self):
        if hasattr(self, 'item_list'):
            self.item_list.update_item_size()

    def resizeEvent(self, event):
        self.update_item_size()
        if hasattr(self, 'item_list'):
            self.item_list.update_style_sheet()

        screen = QApplication.primaryScreen()
        screen_rect = screen.geometry()
        self.setGeometry(0, screen_rect.height() - self.height(), screen_rect.width(), self.height())
        super().resizeEvent(event)


class ContentListWidget(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.setHorizontalScrollMode(QListWidget.ScrollMode.ScrollPerPixel)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setFlow(QListWidget.Flow.LeftToRight)
        self.setWrapping(False)
        self.setViewMode(QListWidget.ViewMode.IconMode)
        self.setResizeMode(QListWidget.ResizeMode.Adjust)
        self.setUniformItemSizes(True)
        self.setSpacing(10)  # 项目间距

        self.update_style_sheet()
        self.update_item_size()

    def update_style_sheet(self):
        if not self.parent:
            return

        # 调整边距和圆角
        base_margin = 5  # 项目之间的外边距
        base_padding = 10  # 项目内边距
        border_radius = 6  # 统一圆角

        self.setStyleSheet(f"""
            QListWidget {{
                background-color: rgba(30, 30, 30, 0);
                border: none;
                border-radius: {border_radius}px;
                padding: 0px;  /* 移除列表内边距，由容器控制边距 */
                margin: 0px;
            }}
            QListWidget::item {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(45, 45, 45, 0.95), stop:1 rgba(55, 55, 55, 0.95));
                border: 1px solid rgba(70, 70, 70, 0.8);
                border-radius: {border_radius}px;
                padding: {base_padding}px;  /* 项目内边距 */
                margin: {base_margin}px;  /* 项目外边距 */
                transition: all 0.3s ease;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
            }}
            QListWidget::item:selected {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgba(55, 55, 55, 0.95), stop:1 rgba(65, 65, 65, 0.95));
                border: 2px solid rgba(100, 100, 100, 0.85);
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
            }}
            QListWidget::item:hover {{
                transform: translateY(-2px);
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
                border-color: rgba(90, 90, 90, 0.9);
            }}
        """)

    def update_item_size(self):
        if not self.parent:
            return

        # 确保正方形数据方框（宽高相等）
        item_width = int(self.parent.width() * 0.12)  # 基于屏幕宽度的12%
        item_height = item_width  # 宽高相等，确保正方形
        self.setIconSize(QSize(item_width - 20, item_width - 20))  # 图标大小适配正方形

    def resizeEvent(self, event):
        self.update_item_size()
        self.update_style_sheet()
        super().resizeEvent(event)
